//  主题首页（昆明直发，品牌严选,早市等）
var basePage = require('../../../common/BasePage'); 
var baseList = require('../../../common/BaseList');
Page({
  behaviors: [basePage, baseList],
  /**
   * 页面的初始数据
   */
  data: {
    /**通告列表 */
    declareArr: [],
    bannerArr: [], 
   
    /**显示加入购物车弹窗 */
    addCart: false,
    /**显示视频弹出层 */
    showVideo: false,
    /**选中的产品 */
    pdc: {},
    /**购买数量 */
    nums: 1,
    reqUrl: '/home/supplier/product/index?wh_id=1',
    /**当前要访问的仓库id */
    wh_id:'',
    reqData: {
      /**颜色id */
      color_id: 0,
      level: '',
    },
    /**颜色列表 */
    colorArr: [{
      color_id: 0,
      title: '全部'
    }],
    /**选中的颜色 */
    color: {
      color_id: 0,
      title: '颜色'
    },
    /**等级数组 */
    levelArr: [],
    /**成熟度 */
    maturityArr:[],
    /**促销选项 */
    typeArr:[
      {
        text: '全部',
        value: ''
      },
      {
        text: '促销',
        value: 3
      },
    ],
    
    /**
     * 排序选项
     */
    orderArr:[
      {
        text: '最新',
        value: 'is_new'
      },
      {
        text: '智能排序',
        value: 'smart'
      },
      {
        text: '人气最高',
        value: 'sales'
      },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('theme onload');
    //获取全局等级数据,成熟度
    this.setData({
      levelArr:[{text: '全部',value: ''},...getApp().globalData.levelArr],
      maturityArr:[{text: '全部',value: '' },...getApp().globalData.maturityArr]
    })
    if (options.title) {
      wx.setNavigationBarTitle({
        title: options.title,
      });
    }
    // console.log("options.this.data.wh_id",options.wh_id);
    let wh_id=options.wh_id||getApp().globalData.warehouse.wh_id;
    this.data.reqUrl='/home/supplier/product/index?wh_id='+wh_id;
    this.data.wh_id=wh_id;
    if(options.type){
      this.data.reqUrl=this.data.reqUrl+"&type="+options.type;
      this.setData({
        typeArr:[]
      })
    }
    
    //请求轮播图
    // this.request({
    //   url: this.data.host + '/home/index/banner?from=warehouse&wh_id='+wh_id
    // }, res => {
    //   let bannerArr = res.data;
    //   bannerArr.map(val => {
    //     val.thumb = this.get_thumb(val.photo, 'wide');
    //   })
    //   this.setData({
    //     bannerArr,
    //   })
    //   //请求通告
    // this.req_declare();
    // });
  },
  req_declare() {
    this.request({
      url: this.data.host + '/home/index/declare?from=warehouse&wh_id='+this.data.wh_id
    }, res => {
      console.log("declare", res);
      this.setData({
        declareArr:res.data
      })
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.current_page = 0;
    
    this.req_items();
  },
  /**
   * 获得列表数据
   */
  get_items_ok(res) {
    this.data.pages = res.pages;
    this.data.current_page = res.current_page;
    console.log("get_items_ok", res);
    res.data.map((res, index, arr) => {
      this.init_val(res, index, arr)
    })
    let itemArr = this.data.itemArr;
    res.current_page == 1 ? itemArr = res.data : itemArr = itemArr.concat(res.data);
    this.setData({
      itemArr,
      colorArr: [{
        color_id: 0,
        title: '全部'
      }, ...res.color_list]
    });
    wx.stopPullDownRefresh({
      success: (res) => {},
    })
  },
  init_val(val, index, arr) {
    val.thumb = this.get_thumb(val.product_img);
  },
  /**
   * 显示商家基地视频
   * @param {event} e 
   */
  show_video(e) {
    // console.log("show_video", e);
    // console.log(this.data.imgHost + '/' + e.detail);
    this.setData({
      showVideo: this.data.imgHost + '/' + e.detail
    })
  },
  /**
   *加入购物车事件
   */
  add_cart_click(e) {
    console.log(e);
    this.setData({
      addCart: true,
      pdc: e.detail,
    });
  },
  /**
   * 添加产品到购物车
   */
  add_cart() {
    this.request({
      url: this.data.host + '/home/cart/addToCart',
      data: {
        pid: this.data.pdc.pid,
        nums: this.data.nums
      },
      method: 'post'
    }, this.add_cart_ok);
  },
  add_cart_ok(res) {
    wx.showToast({
      title: res.msg,
      icon: 'success'
    })
  },
  nums_change(e) {
    this.data.nums = e.detail;
    // console.log("nums_change",e)
  },
  /**
   * 收藏点击
   */
  collect_click(e) {
    console.log("collect_click", e);
    let pdc = e.detail;
    let index = e.currentTarget.dataset.index;
    let status = pdc.collected ? 0 : 1;
    this.request({
      url: this.data.host + '/home/user/collect/collect',
      data: {
        status: status,
        can_id: pdc.pid,
        type: 1
      },
      method: 'post'
    }, res => {
      console.log("collect_ok", res);
      let item = this.data.itemArr[index];
      item.collected = status;
      item.collects = status ? (item.collects * 1) + 1 : item.collects - 1;
      this.setData({
        itemArr: this.data.itemArr
      })
    });
  },
  /**选中颜色 */
  color_click(e) {
    console.log("color_click", e);
    let reqData = this.data.reqData;
    let color = e.currentTarget.dataset.color;
    reqData.color_id = color.color_id;
    this.setData({
      reqData,
      color
    });
    this.selectComponent('#color').toggle();
    /**更新列表 */
    this.data.current_page = 0;
    this.req_items();
  },
  /**
   * 筛选项变化
   */
  req_change(e) {
    // console.log("level_change", e);
    this.data.reqData[e.currentTarget.dataset.field]=e.detail;
    this.data.current_page = 0;
    this.req_items();
  },
  /**确认搜索内容 */
  search_click(e){
    console.log("search_click",e);
    this.data.current_page = 0;
    this.data.reqData={
      so:e.detail
    }
    this.req_items();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log("theme onReady" );
  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },




  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    console.log("onShareAppMessage",res);
    let pid=res.target.dataset.pid;
    let product_id=res.target.dataset.product_id;
    let path="/pages/cls/theme/Index";
    let title="";
    if(pid){
      path='/pages/cls/pdcList/Index?pid='+pid+'&product_id=' + product_id;
      title="您的朋友向你推荐了一款鲜花"
    }
    return {
      title: title,
      path: path,
      // promise 
    }
  }
})